package com.ocom.human.controller;


import com.ocom.common.constant.HumanServiceUrl;
import com.ocom.common.entity.human.EntityPersonTag;
import com.ocom.common.entity.human.EntityPersonTagRelation;
import com.ocom.common.entity.human.EntityPersonTagType;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.request.human.ManagePerTagRelationRequest;
import com.ocom.common.request.human.RemovePerTagGroupRequest;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.human.service.PersonTagRelationService;
import com.ocom.human.service.PersonTagService;
import com.ocom.human.service.PersonTagTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.stream.Collectors;

@RestController
public class PersonTagController {

    @Autowired
    PersonTagService personTagService;

    @Autowired
    private PersonTagTypeService personTagTypeService;

    @Autowired
    private PersonTagRelationService personTagRelationService;

    @PostMapping(HumanServiceUrl.URL_getPerTag)
    public Result getPerTag(Long comId, @RequestParam(name = "state", required = false, defaultValue = "0") Integer state, @RequestParam(name = "groupNo", required = false, defaultValue = "0") Integer groupNo) {
        return ResultUtil.success(personTagService.list(comId, state, groupNo));
    }

    @PostMapping(HumanServiceUrl.URL_addPerTag)
    public Result addPerTag(@RequestBody EntityPersonTag entityPersonTag) {
        Integer bo = personTagService.addPersonTag(entityPersonTag.getComId(), entityPersonTag);

        if (bo == -1) {
            return ResultUtil.error(ResultEnum.Err_3087);
        }
        if (bo == 0) {
            return ResultUtil.error(ResultEnum.Err_2016);
        }

        return ResultUtil.success();
    }

    @PostMapping(HumanServiceUrl.URL_updatePerTag)
    public Result updatePerTag(@RequestBody EntityPersonTag entityPersonTag) {
        Integer bo = personTagService.updatePersonTag(entityPersonTag.getComId(), entityPersonTag);

        if (bo == -1) {
            return ResultUtil.error(ResultEnum.Err_3087);
        }
        if (bo == 0) {
            return ResultUtil.error(ResultEnum.Err_3014);
        }

        return ResultUtil.success();
    }

    @PostMapping(HumanServiceUrl.URL_getPerTagGroup)
    public Result getPerTagGroup(@RequestParam("comId") Long comId) {

        return ResultUtil.success(personTagTypeService.getPerTagGroup(comId));
    }

    @PostMapping(HumanServiceUrl.URL_addPerTagGroup)
    public Result addPerTagGroup(@RequestParam("comId") Long comId, @RequestBody EntityPersonTagType entityPersonTagType) {
        boolean b = personTagTypeService.addPerTagGroup(comId, entityPersonTagType);
        if (!b) {
            return ResultUtil.error(ResultEnum.Err_4300);
        }
        return ResultUtil.success();
    }

    @PostMapping(HumanServiceUrl.URL_updatePerTagGroup)
    public Result updatePerTagGroup(@RequestParam("comId") Long comId, @RequestBody EntityPersonTagType entityPersonTagType) {
        int result = personTagTypeService.updatePerTagGroup(comId, entityPersonTagType);

        if (-2 == result) {
            return ResultUtil.error(ResultEnum.Err_4301);
        }

        if (0 == result) {
            return ResultUtil.error(ResultEnum.Err_3014);
        }

        return ResultUtil.success();
    }

    @PostMapping(HumanServiceUrl.URL_removePerTagGroup)
    public Result removePerTagGroup(@RequestBody RemovePerTagGroupRequest request) {

        int count = personTagService.countPersonTag(request.getComId(), request.getGroupNo());
        Map<String, Object> map = new HashMap();
        map.put("useNums", count);

        if (0 < count && 0 == request.getType()) {
            return ResultUtil.error(ResultEnum.Err_4302, map);
        }

        if (0 == count || 1 == request.getType()) {
            personTagService.removePerTagGroup(request.getComId(), request.getGroupNo(), request.getUserName() + ":删除了标签组");
            personTagTypeService.removePerTagGroup(request.getComId(), request.getGroupNo());
        }

        return ResultUtil.success();
    }

    @PostMapping(HumanServiceUrl.URL_managePerTagRelation)
    public Result managePerTagRelation(@RequestBody ManagePerTagRelationRequest request) {
        List<EntityPersonTagRelation> relationList = personTagRelationService.getByComId(request.getComId());
        Map<String, EntityPersonTagRelation> relationMap = relationList.stream().collect(Collectors.toMap(e -> (e.getPId() + ":" + e.getTagId()), e -> e));
        List<Long> ids = new ArrayList<>();
        Set<Long> pIds = new HashSet<>();
        List<EntityPersonTagRelation> entityList = new ArrayList<>();

        if (2 == request.getType() && 0 < request.getPerLists().size()) {
            pIds.add(request.getPerLists().get(0).getPId());
        } else {
            for (ManagePerTagRelationRequest.PerList perList : request.getPerLists()) {
                if(perList.getPerTags()==null || perList.getPerTags().size()==0){
                    pIds.add(perList.getPId());
                    continue;
                }
                for (ManagePerTagRelationRequest.PerTag perTag : perList.getPerTags()) {
                    EntityPersonTagRelation mapRelation = relationMap.get(perList.getPId() + ":" + perTag.getTagId());

                    EntityPersonTagRelation entityPersonTagRelation = new EntityPersonTagRelation();
                    entityPersonTagRelation.setComId(request.getComId());
                    entityPersonTagRelation.setPId(perList.getPId());
                    entityPersonTagRelation.setTagId(perTag.getTagId());

                    if (1 == request.getType()) {
                        pIds.add(perList.getPId());
                        entityList.add(entityPersonTagRelation);
                    } else if (0 == request.getType()) {
                        if (null == mapRelation) {
                            entityList.add(entityPersonTagRelation);
                        }
                    }
                }
            }
        }

        if (0 < ids.size()) {
            personTagRelationService.batchDeleteById(request.getComId(), ids);
        }
        if (0 < pIds.size()) {
            personTagRelationService.batchDeleteByPId(request.getComId(), pIds);
        }
        if (0 < entityList.size()) {
            personTagRelationService.batchSave(request.getComId(), entityList);
        }

        return ResultUtil.success();
    }

}
